package org.example.domain.strategy.service.rule.tree.factory.engine.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import org.example.domain.strategy.model.vo.RuleTreeNodeLineVO;
import org.example.domain.strategy.model.vo.RuleTreeNodeVO;
import org.example.domain.strategy.model.vo.RuleTreeVO;
import org.example.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.example.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.example.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;

import java.util.List;
import java.util.Map;

@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {

    private final Map<String, ILogicTreeNode> groups;
    private final RuleTreeVO ruleTreeVO;


    public DecisionTreeEngine(Map<String, ILogicTreeNode> groups, RuleTreeVO ruleTreeVO) {
        this.ruleTreeVO = ruleTreeVO;
        this.groups = groups;
    }

    @Override
    public DefaultTreeFactory.StrategyAwardData process(String userId, Long strategyId, Integer awardId) {

        DefaultTreeFactory.StrategyAwardData strategyAwardData = null;

        //由测试数据填充根节点、其他节点等信息
        String nextNode = ruleTreeVO.getTreeRootRuleNode();
        Map<String, RuleTreeNodeVO> treeNodeMap = ruleTreeVO.getTreeNodeMap();

        //获取起始节点
        RuleTreeNodeVO ruleTreeNodeVO = treeNodeMap.get(nextNode);
        while (nextNode != null) {

            //获取节点具体实现类对象
            ILogicTreeNode logicEntity = groups.get(ruleTreeNodeVO.getRuleKey());
            String ruleValue = ruleTreeNodeVO.getRuleValue();

            DefaultTreeFactory.TreeActionEntity treeActionEntity = logicEntity.logic(userId, strategyId, awardId, ruleValue);

            RuleLogicCheckTypeVO ruleLogicCheckType = treeActionEntity.getRuleLogicCheckType();
            strategyAwardData = treeActionEntity.getStrategyAwardData();
            log.info("决策树引擎: treeId:{} node:{} code:{}", ruleTreeVO.getTreeId(), ruleTreeNodeVO.getRuleKey(), ruleLogicCheckType.getCode());

            //获取下一个节点
            nextNode = nextNode(ruleLogicCheckType.getCode(), ruleTreeNodeVO.getTreeNodeLineVOList());
            ruleTreeNodeVO = treeNodeMap.get(nextNode);
        }
        return strategyAwardData;
    }

    private String nextNode(String code, List<RuleTreeNodeLineVO> treeNodeLineVOList) {
        if (null == treeNodeLineVOList || treeNodeLineVOList.isEmpty()) return null;
        for (RuleTreeNodeLineVO ruleTreeNodeLineVO : treeNodeLineVOList) {
            if (decisionLogic(code, ruleTreeNodeLineVO)) {
                return ruleTreeNodeLineVO.getRuleNodeTo();
            }
        }
//        throw new RuntimeException("决策树引擎，nextNode 计算失败，未找到可执行节点！");
        return  null;
    }

    public boolean decisionLogic(String matterValue, RuleTreeNodeLineVO nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());
            // 以下规则暂时不需要实现
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }
}
